<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* DB Handler
*
* Functions Regarding Database Interactions and Abstraction
* 
* Provides the Crucial Bridge Layer Between "The Game" and the Database
* Architecture (This is our "Glue")
*/

/* Currently implementing a MySQL database */
class dbHandler {

	public static $link;
	
	public static $hostname;
	public static $username;
	public static $pass;
	public static $database;
	
	public static function connect($hostname, $username, $pass, $database = "kusht") {
		dbHandler::$hostname = $hostname;
		dbHandler::$username = $username;
		dbHandler::$pass	 = $pass;
		dbHandler::$database = $database;	
		
		dbHandler::reconnect();
	}
	
	public static function reconnect() {
		dbHandler::$link = mysqli_connect(dbHandler::$hostname, dbHandler::$username, dbHandler::$pass);
		mysqli_select_db(dbHandler::$link,dbHandler::$database) or die( "Unable to select database");
		if (!dbHandler::is_installed()) { dbHandler::install_db(); }	
	}

	// Of course, everything above is aspirational. ;)
	public static function loadGame() {
		dbHandler::loadRooms();
		World::sortRooms();

		dbHandler::loadPlacedItems();
		
		dbHandler::loadItemLibrary();
		echo "---\nDatabase Loaded";
	}
	
	public static function save($object) {
		// We are passed any in-game object, and we save it by
		// Retrieving it's class and tossing it off to its own specific saver
		$class = get_class($object);
		$func_name = "save".$class;
		dbHandler::$func_name($object);
	}
	
	public static function load($object, $id) {
		// We are passed any in-game object, and we load it by
		// Retrieving it's class and tossing it off to its own specific loader
		$class = get_class($object);
		$func_name = "load".$class;
		$object = dbHandler::$func_name($id);
		return $object;
	}
	
	public static function delete($object) {
		// We are passed any in-game object, and we delete it by
		// Retrieving it's class and tossing it off to its own specific deleter
		$class = get_class($object);
		$func_name = "delete".$class;
		dbHandler::$func_name($object);
	}
	
	public static function retain($object) {
		// We are passed anything in the in-game Item branch, and we retain it by
		// Retrieving it's class and tossing it off to its own specific retainer
		$class = get_class($object);
		$func_name = "retain".$class;
		dbHandler::$func_name($object);	
	}
	
	public static function grab($object, $id) {
		// We are passed anything in the in-game Item branch, and we grab it by
		// Retrieving it's class and tossing it off to its own specific grabber
		$class = get_class($object);
		$func_name = "grab".$class;
		$object = dbHandler::$func_name($id);
		return $object;
	}
	
	public static function erase($object) {
		// We are passed anything in the in-game Item branch, and we erase it by
		// Retrieving it's class and tossing it off to its own specific eraser
		$class = get_class($object);
		$func_name = "erase".$class;
		dbHandler::$func_name($object);	
	}
	
	/* ------------ STARTUP-ONLY FUNCTIONS --------------------- */
	
	// Rooms and Exits...
	public static function loadRooms() {
		$query="SELECT template_id FROM rooms";
		$result = mysqli_query(dbHandler::$link,$query);
		
		while ($row = mysqli_fetch_row($result)) {
        	$room = dbHandler::loadstartupRoom($row[0]);
			World::$room_list += array($room->copy_id=>$room);
    	}
		
		dbHandler::loadAllExits_();
		
		foreach(World::$room_list as $room) {
			World::orderExits($room->exits);
		}
	}
	
	public static function loadstartupRoom($id) {
		$room = new Room();
		set_fields_equal($room,dbHandler::loadBaseObject($id));
		return $room;
	}
	
	// Run once on startup
	public static function loadAllExits_() {
		$query = "SELECT template_id FROM exits";
		$result = mysqli_query(dbHandler::$link,$query); 
		
		while ($row = mysqli_fetch_row($result)) {
        	$exit = dbHandler::loadExit_($row[0]);

			$key = $exit->in_object_id;
			$in_room = World::$room_list[$key];
			
			
			$key = $exit->to_room_id;
			$exit->to_room = World::$room_list[$key];
			
			if(!$in_room) { continue; } // We drew a bad room TODO: Delete these exits
			$in_room->exits += array($exit->copy_id=>$exit);
    	}
	}
	
	// Placed/Perm Items (fountains, immobile tables, signs, etc.)
	public static function loadPlacedItems() {
		$query = "SELECT copy_id FROM item_instances";
		$result = mysqli_query(dbHandler::$link,$query); 
		while ($row = mysqli_fetch_row($result)) {
        	$item = dbHandler::grabItem($row[0]);
						
			$key = $item->in_object_id;			
			$in_room = World::$room_list[$key];
			
			$in_room->items += array($item->copy_id=>$item);
    	}
	}
	
	// Libraries...
	public static function loadItemLibrary() {
		$query="SELECT template_id FROM items";
		$result = mysqli_query(dbHandler::$link,$query);
		
		while ($row = mysqli_fetch_row($result)) {
        	$item = dbHandler::loadItem($row[0]);
			World::$items += array($item->copy_id=>$item);
    	}
	}
	
	/* ------------ END STARTUP-ONLY FUNCTIONS ------------------ */
	
	public static function saveRoom($room) {
		// We are going to be inserting a new BaseObject that represents this room's Information
		dbHandler::saveBaseObject($room);
		// Now we save the room as a room
		$query="INSERT INTO rooms (template_id) VALUES ('$room->template_id')";
		mysqli_query(dbHandler::$link,$query);
		// Now we save the exits as exits
		foreach($room->exits as $exit) {
			$exit->in_object = $room; // Just to be certain
			dbHandler::saveExit_($exit);
		}
		
	}
	
	public static function loadRoom($id) {
		$room = new Room();
		set_fields_equal($room,dbHandler::loadBaseObject($id));
		dbHandler::loadRoomExits_($room);
		return $room;
	}
	
	// Used in the context of loadRoom($id)
	public static function loadRoomExits_($room) {
		$id = $room->copy_id;
		$query = "SELECT * FROM baseobjects,exits WHERE baseobjects.in_object_id='$id' AND baseobjects.copy_id = exits.template_id";
		$result = mysqli_query(dbHandler::$link,$query) or die("Bad Query @ loadRoomExit");

		while ($row = mysqli_fetch_row($result)) {
        	$exit = dbHandler::loadExit_($row[0]);
			$in_room = $room;
			$exit->to_room = get_obj_by_field_value($exit->to_room_id,"id",World::$room_list);
			$exit->in_object = $in_room;
			$in_room->exits += array($exit->copy_id=>$exit);
    	}
	}
	
	public static function saveExit_($exit) {
		dbHandler::saveBaseObject($exit);
		add_slashes_object($exit);
		
		// Prep a couple longer chains for query
		$tmp1 = $exit->to_room->copy_id;
		// Query String
		$query="INSERT INTO exits (template_id,tellUser,tellOthers,command,commandtgt,to_room_id) 
		VALUES ('$exit->template_id', '$exit->tellUser', '$exit->tellOthers', '$exit->command', '$exit->commandtgt',
		'$tmp1') ON DUPLICATE KEY UPDATE tellUser=VALUES(tellUser), tellOthers=VALUES(tellOthers), command=VALUES(command),
		commandtgt=VALUES(commandtgt), to_room_id=VALUES(to_room_id)";
		mysqli_query(dbHandler::$link,$query) or die("Failed to Insert or Update exits Table\n\n".mysqli_error(dbHandler::$link));
		
		// Remove Slashes
		rmv_slashes_object($exit);
	}
	
	public static function loadExit_($id) {
		$exit = new Exit_();
		set_fields_equal($exit,dbHandler::loadBaseObject($id));
		$query = "SELECT * FROM exits WHERE template_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query); 
		$obj = mysqli_fetch_object($result);
		set_fields_equal($exit,$obj);		
		return $exit;
	}
	
	public static function deleteExit_($exit) {
		$id = $exit->template_id;
		$query = "DELETE FROM exits WHERE template_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete Exit from exit table"); 
		dbHandler::deleteBaseObject($exit);
	}
	
	public static function saveItem($item) {
		dbHandler::saveBaseObject($item);
		$query="INSERT INTO items (template_id) VALUES ('$item->template_id')";
		mysqli_query(dbHandler::$link,$query);
	}
	
	public static function loadItem($id) {
		$item = new Item();
		set_fields_equal($item,dbHandler::loadBaseObject($id));	
		return $item;	
	}
	
	public static function retainItem($item) {
		dbHandler::retainBaseObject($item);
		$query="INSERT INTO item_instances (copy_id) VALUES ('$item->copy_id')";
		mysqli_query(dbHandler::$link,$query);
	}
	
	public static function grabItem($id) {
		$item = new Item();
		set_fields_equal($item,dbHandler::grabBaseObject($id));	
		return $item;		
	}
	
	public static function deleteItem($item) {
		$id = $item->template_id;
		$query = "DELETE FROM items WHERE template_id='$id'";		
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete $id from items table"); 
		dbHandler::deleteBaseObject($item);	
	}
	
	public static function eraseItem($item) {
		$id = $item->copy_id;
		$query = "DELETE FROM items WHERE copy_id='$id'";		
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete $id from item_instances table"); 
		dbHandler::eraseBaseObject($item);	
	}

	// BASEOBJECTS
	public static function saveBaseObject($object) {
		add_slashes_object($object);
		// Long Chains
		if(!isset($object->in_object)) { $container_id = DEF_NEG; }
		else { $container_id = $object->in_object->copy_id; }
		// Query
		$query="INSERT INTO baseobjects (template_id, copy_id, name, short_descr, description, thumb, image, weight, in_object_id, set_name) 
				VALUES ('$object->template_id','$object->copy_id','$object->name', '$object->short_descr', '$object->description', '$object->thumb', '$object->image', 
				'$object->weight', '$container_id','$object->set_name') ON DUPLICATE KEY UPDATE name=VALUES(name), short_descr=VALUES(short_descr), 
				description=VALUES(description), thumb=VALUES(thumb), image=VALUES(image), weight=VALUES(weight), in_object_id=VALUES(in_object_id), 
				set_name=VALUES(set_name)";
		
		mysqli_query(dbHandler::$link,$query) or die("Failed to Insert or Update BaseObjects Table\n\n".mysqli_error(dbHandler::$link));
		
		// Send its Flags to the Flags table
		dbHandler::saveFlags($object);
		
		// Remove Slashes
		rmv_slashes_object($object);
	}
	
	public static function loadBaseObject($id) {
		$query = "SELECT * FROM baseobjects WHERE template_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query); 
		$obj = mysqli_fetch_object($result);
		$object = new BaseObject();
		set_fields_equal($object,$obj);
		dbHandler::loadFlags($object);
		dbHandler::loadListenersForObject($object);
		return $object;
	}
	
	public static function retainBaseObject($object) {
		add_slashes_object($object);
		// Long Chains
		if(!isset($object->in_object)) { $container_id = DEF_NEG; }
		else { $container_id = $object->in_object->copy_id; }
		// Query
		$query="INSERT INTO baseobject_instances (template_id, copy_id, name, short_descr, description, thumb, image, weight, in_object_id, set_name) 
				VALUES ('$object->template_id','$object->copy_id','$object->name', '$object->short_descr', '$object->description', '$object->thumb', '$object->image', 
				'$object->weight', '$container_id','$object->set_name') ON DUPLICATE KEY UPDATE name=VALUES(name), short_descr=VALUES(short_descr), 
				description=VALUES(description), thumb=VALUES(thumb), image=VALUES(image), weight=VALUES(weight), in_object_id=VALUES(in_object_id), 
				set_name=VALUES(set_name)";
		
		mysqli_query(dbHandler::$link,$query) or die("Failed to Insert or Update BaseObject_Instances Table\n\n".mysqli_error(dbHandler::$link));
		
		// Send its Flags to the Flags table
		dbHandler::saveFlags($object);
		
		// Remove Slashes
		rmv_slashes_object($object);	
	}
	
	public static function grabBaseObject($id) {
		$query = "SELECT * FROM baseobject_instances WHERE copy_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query); 
		$obj = mysqli_fetch_object($result);
		$object = new BaseObject();
		set_fields_equal($object,$obj);
		dbHandler::loadFlags($object);
		dbHandler::loadListenersForObject($object);
		return $object;
	}
	
	public static function deleteBaseObject($object) {
		$id = $object->template_id;
		$query = "DELETE FROM baseobjects WHERE template_id='$id'";		
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete $id from baseobjects table"); 
		dbHandler::deleteFlags($object);
	}
	
	public static function eraseBaseObject($object) {
		$id = $object->copy_id;
		$query = "DELETE FROM baseobject_instances WHERE copy_id='$id'";		
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete $id from baseobject_instances table"); 
		dbHandler::deleteFlags($object);
	}
	
	// FLAGS
	public static function saveFlags($object) {
		if(!isset($object->all_flags)) { return; }
		
		foreach($object->all_flags as $k=>$e) {
			$query = "INSERT INTO flags (copy_id, flag, byte_num) VALUES ('$object->copy_id', '$e', '$k')";
			mysqli_query(dbHandler::$link,$query) or die('Unable to save Flags');
		}
	}
	
	public static function loadFlags($object) {
		$query = "SELECT * FROM flags WHERE copy_id='$object->copy_id'";
		$result = mysqli_query(dbHandler::$link,$query);
		
		while ($row = mysqli_fetch_row($result)) {
			$object->all_flags[$row[3]] = $row[2]; // $row[3] is the index number and $row[2] is the flag value
		}
	}
	
	public static function deleteFlags($object) {
		$id = $object->copy_id;
		$query = "DELETE FROM flags WHERE copy_id='$id'";		
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to delete $id from flags table"); 	
	}
	
	public static function saveLiquid($object) {
		if(!isset($object->id)) {
			$query = "INSERT INTO liquids (name, adjective, mod_drunk, mod_hunger, mod_thirst) VALUES 
			('$object->name', '$object->adjective', '$object->mod_drunk', '$object->mod_hunger', '$object->mod_thirst')";
			mysqli_query(dbHandler::$link,$query) or die('Unable to save Liquid');
		} else {
			$query = "UPDATE liquids SET name='$object->name', adjective='$object->adjective', mod_drunk='$object->mod_drunk', 
			mod_hunger='$object->mod_hunger', mod_thirst='$object->mod_thirst' WHERE id='$object->template_id'";
			mysqli_query(dbHandler::$link,$query) or die('Unable to update Liquid');
		}
	}
	
	public static function loadLiquid($id) {
		$query = "SELECT * FROM liquids WHERE id='$id'";
		$result = mysqli_query(dbHandler::$link,$query) or die("unable to select any liquids"); //TODO: Fix this
		$obj = mysqli_fetch_object($result);
		$l = new Liquid();
		set_fields_equal($l,$obj);
		return $l;
	}
	
	/* ------------ LOGIN ACCOUNT ----------------- */
	
	public static function charExists($name) {
		$query = "SELECT * FROM user_characters, baseobjects WHERE baseobjects.name='$name'";
		return mysqli_num_rows(mysqli_query(dbHandler::$link,$query));
	}
	
	public static function loadUserAccount($id) {
		$query = "SELECT * FROM user_accounts WHERE id='$id'";
		$result = mysqli_query(dbHandler::$link,$query) or die("Unable to load User Account");
		if(mysqli_num_rows($result)==0) { return false; }
		return mysqli_fetch_object($result);
	}
	
	public static function saveUserAccount($id,$password,$email) {
		$query = "INSERT INTO user_accounts (id, password, email) VALUES
			('$id','$password','$email')";
		$result = mysqli_query(dbHandler::$link,$query);
	}
	
	public static function loadUserCharacters($id) {
		$query = "SELECT * FROM user_characters WHERE owner_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query);
		if(mysqli_num_rows($result)==0) { return false; }
		$tmp = array();
		$i = 0;
		while ($row = mysqli_fetch_row($result)) {
			$tmp += array($i=>array("id"=>$row[0]));
			$i++;
		}
		return $tmp;
	}
	
	// To be updated and expanded
	public static function loadUserCharacter($id) {
		$tmp = new UserCharacter();
		$obj = dbHandler::loadBaseObject($id);
		set_fields_equal($tmp,$obj);
		$query = "SELECT * FROM user_characters WHERE template_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query);
		$obj = mysqli_fetch_object($result);
		set_fields_equal($tmp,$obj);
		return $tmp;
	}
		
	// This function will need to be updated and expanded
	public static function saveUserCharacter($userchar) {
		dbHandler::saveBaseObject($userchar);
		$query = "INSERT INTO user_characters (template_id, owner_id) VALUES ('$userchar->template_id','$userchar->owner_id')
		ON DUPLICATE KEY UPDATE owner_id=VALUES(owner_id)";
		mysqli_query(dbHandler::$link,$query) or die("Unable to save UserCharacter\n\n".mysqli_error(dbHandler::$link));
	}
	
	public static function saveListener($listener) {
		add_slashes_object($listener);
		$copy_id = $listener->a_object->copy_id;
		$query = "INSERT INTO listeners (copy_id,script,trigr) VALUES ('$copy_id','$listener->script','$listener->trigr')
		ON DUPLICATE KEY UPDATE copy_id=VALUES(copy_id), script=VALUES(script), trigr=VALUES(trigr)";
		mysqli_query(dbHandler::$link,$query) or die("Unable to save Listener\n\n".mysqli_error(dbHandler::$link));	
		rmv_slashes_object($listener);		
	}
	
	public static function loadListener($id) {
		$query = "SELECT * FROM listeners WHERE listener_id='$id'";
		$result = mysqli_query(dbHandler::$link,$query);	
		$obj = mysqli_fetch_object($result);
		$tmp = new Listener();
		set_fields_equal($tmp,$obj);
		return $tmp;
	}
	
	public static function loadListenersForObject($object) {
		$copy_id = $object->copy_id;
		$query = "SELECT * FROM listeners WHERE copy_id='$copy_id'";
		$result = mysqli_query(dbHandler::$link,$query);	
		while ($row = mysqli_fetch_row($result)) {
			$tmp = dbHandler::loadListener(current($row));
			$tmp->attach($tmp->trigr,$object);	
		}	
	}
	
	/* ------------ INSTALLATION STUFF ------------ */
	
	function is_installed() {
		$installed = mysqli_query(dbHandler::$link, "SELECT * FROM installed" );
		if ( $installed ) { return true; }		
	}
	
	public static function delete_db() {
		mysqli_query(dbHandler::$link,"DROP DATABASE kusht");
		mysqli_query(dbHandler::$link,"CREATE DATABASE kusht");
		mysqli_select_db(dbHandler::$link,dbHandler::$database) or die( "Unable to select database");
	}
	
	/* All game objects' IDs are based on the unique ID Assigned
	   by baseobject table */
	public static function install_db() {
		// Create the BaseObjects Table
		$query="CREATE TABLE baseobjects (
				template_id			VARCHAR(23)	NOT NULL,
				copy_id				VARCHAR(23)	NOT NULL,
				name				TINYTEXT	NOT NULL,
				short_descr			TEXT		NOT NULL,
				description			TEXT		NOT NULL,
				thumb				TINYTEXT	NOT NULL,
				image				TINYTEXT	NOT NULL,
				weight				SMALLINT	NOT NULL,
				in_object_id		VARCHAR(23)	NOT NULL,
				set_name			TINYTEXT	NOT NULL,

				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id),

				UNIQUE					(copy_id),
				KEY						(copy_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table baseobjects created.\n";
		}; 

		// Create the Items Table
		$query="CREATE TABLE items (
				template_id		VARCHAR(23)	NOT NULL,	
				
				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table items created.\n";
		}; 
		
		// Create the Flags Table
		$query="CREATE TABLE flags (
				flag_id			int(6) 		NOT NULL auto_increment,
				copy_id			VARCHAR(23)	NOT NULL,
				flag			INT			NOT NULL,
				byte_num		int(6)		NOT NULL,
				
				PRIMARY KEY 			(flag_id),
				UNIQUE 			flag_id	(flag_id),
				KEY 			id_2 	(flag_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table flags created.\n";
		}; 
		
		// Create the Listeners Table (AKA Scripting)
		$query="CREATE TABLE listeners (
				listener_id		int(6) 		NOT NULL auto_increment,
				copy_id			VARCHAR(23)	NOT NULL,
				script			TEXT		NOT NULL,
				trigr			TEXT		NOT NULL,
				
				PRIMARY KEY 				(listener_id),
				UNIQUE 			listener_id	(listener_id),
				KEY 			id_2 		(listener_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table listeners created.\n";
		}; 
	
		// Create the Rooms Table
		$query="CREATE TABLE rooms (
				template_id		VARCHAR(23)	NOT NULL,	
				
				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table rooms created.\n";
		}; 
		
		// Create the Exits Table
		$query="CREATE TABLE exits (
				template_id		VARCHAR(23)	NOT NULL,
				tellUser		TINYTEXT	NOT NULL,
				tellOthers		TINYTEXT	NOT NULL,
				command			TINYTEXT	NOT NULL,
				commandtgt		TINYTEXT	NOT NULL,
				to_room_id		VARCHAR(23)	NOT NULL,
				
				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table exits created.\n";
		}; 
		
		// Create the User Accounts Table
		$query="CREATE TABLE user_accounts (
				id			varchar(20) NOT NULL,
				password	varchar(20) NOT NULL,
				email 		varchar(30) NOT NULL,
				
				PRIMARY KEY 			(id),
				UNIQUE 			id 		(id),
				KEY 			id_2 	(id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table user_accounts created.\n";
		}; 
		
		// Create the User Character Table
		$query="CREATE TABLE user_characters (
				template_id		VARCHAR(23)	NOT NULL,
				owner_id		varchar(20)	NOT NULL,
				
				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table user_characters created.\n";
		}; 
		
		// Create the Meta Text Table
		// Meta Text is notes, help files, ideas, etc.
		// Human Readable: SELECT DATE_FORMAT(timestamp,'%l:%i:%s %p on %M %D, %Y') AS ... FROM ... WHERE ...
		$query="CREATE TABLE meta_text (
				id 				int(6) 		NOT NULL auto_increment,
				type			INT			NOT NULL,
				topic			TINYTEXT	NOT NULL,
				author			TINYTEXT	NOT NULL,
				keywords		TINYTEXT	NOT NULL,
				firstposted		TIMESTAMP	DEFAULT NOW(),
				lastrevised		TIMESTAMP,
				content			TEXT		NOT NULL,
				
				PRIMARY KEY 			(template_id),
				UNIQUE 			id 		(template_id),
				KEY 			id_2 	(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table meta_text created.\n";
		}; 
		
		// Create the BaseObject Instances Table
		$query="CREATE TABLE baseobject_instances (
				copy_id			VARCHAR(23)	NOT NULL,
				template_id		VARCHAR(23)	NOT NULL,
				name			TINYTEXT	NOT NULL,
				short_descr		TEXT		NOT NULL,
				description		TEXT		NOT NULL,
				thumb			TINYTEXT	NOT NULL,
				image			TINYTEXT	NOT NULL,
				weight			SMALLINT	NOT NULL,
				in_object_id	VARCHAR(23)	NOT NULL,
				set_name		TINYTEXT	NOT NULL,

				PRIMARY KEY 					(copy_id),
				UNIQUE 			copy_id 		(copy_id),
				KEY 			id_2 			(copy_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table baseobject_instances created.\n";
		}; 
		
		// Create the Item Instances Table
		$query="CREATE TABLE item_instances (
				copy_id 		VARCHAR(23)		NOT NULL,

				PRIMARY KEY 					(copy_id),
				UNIQUE 			copy_id 		(copy_id),
				KEY 			id_2 			(copy_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table item_instances created.\n";
		}; 
		
		// Create the Liquids Table
		$query="CREATE TABLE liquids (
				id				int(6) 		NOT NULL auto_increment,
				name			TINYTEXT	NOT NULL,
				adjective		TINYTEXT	NOT NULL,
				mod_drunk		TINYINT		NOT NULL,
				mod_hunger		TINYINT		NOT NULL,
				mod_thirst		TINYINT		NOT NULL,
				
				PRIMARY KEY 			(id),
				UNIQUE 			id		(id),
				KEY 			id_2 	(id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Table liquids created.\n";
		}; 
	
		// Create an "installed" table to verify installation
		$query="CREATE TABLE installed (
				id 			int(6) 		NOT NULL auto_increment,
				
				PRIMARY KEY 			(template_id))";
		if (mysqli_query(dbHandler::$link,$query) === TRUE) {
			echo "Database Installed.\n\n";
		}; 
	}
	
}

?>